<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.util.*,java.text.SimpleDateFormat,com.brother.Constants,
com.brother.model.*,com.ibatis.common.util.PaginatedList
,com.brother.util.DateFormatter,com.brother.model.AlarmType,com.brother.util.CountryLocation, com.brother.service.*"%><%
com.brother.model.Customer user = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(user == null){
	return;
}
String trackerid = request.getParameter("trackerid");
String date = request.getParameter("date");
String interval = request.getParameter("interval");
java.text.DecimalFormat nf =new java.text.DecimalFormat("0.00");  
System.out.println("trackerid=" + trackerid + ", date=" + date+",interval=" + interval);

String dateFormat = (String)session.getAttribute(Constants.DATE_FORMAT_SESSION_NAME);
SimpleDateFormat formatEn = new SimpleDateFormat("dd/MM/yyyy"); // English date format
SimpleDateFormat formatPT = new SimpleDateFormat("dd/MM/yyyy"); // Portul date format
SimpleDateFormat sessionFormat = new SimpleDateFormat(dateFormat);

SimpleDateFormat formatCN = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 


String extDateFormat = (String)session.getAttribute(Constants.EXT_DATE_FORMAT_SESSION_NAME);

DateFormatter localFormat = new DateFormatter(); // 输出时间
System.out.println("user time zone=" + user.getTimeZoneOffSet());
localFormat.setConfig(user.getTimeZoneOffSet(), "HH:mm");

if(!DateFormatter.isChineaseFormat(date)){ // 非中文格式的日期
	date = formatCN.format(sessionFormat.parse(date));
}
// 换成东八区时间
date = DateFormatter.toEastEight(formatCN.parse(date), user.getTimeZoneOffSet(), "yyyy-MM-dd HH:mm:ss");
Date tempDate = formatCN.parse(date);
tempDate.setHours(tempDate.getHours() + 24);
String endDate = formatCN.format(tempDate);
ReportService rsv = new ReportService();
List<Report> result = rsv.findAllByRange(date, endDate , trackerid);
System.out.println("data size=" + result.size());
StringBuilder sb = new StringBuilder();
StringBuilder label = new StringBuilder();
sb.append("[");
label.append("[");
int timeInterval = 5;
if(interval.equalsIgnoreCase("10m")){
	timeInterval = 10;
}else if(interval.equalsIgnoreCase("20m")){
	timeInterval = 20;
}else if(interval.equalsIgnoreCase("1h")){
	timeInterval = 60;
}
Date dateObject = formatCN.parse(date);
long span = 0;
boolean hit = false;
long increase = timeInterval * 60 * 1000;
SimpleDateFormat timeFT = new SimpleDateFormat("HH:mm"); 
Date time = new Date();
float speed = 0; // 速度总数
int count = 0;// 速度计算次数
int minute = 0;
int totalColumns = 0;
int currentCol = 1; // 当前柱子
for(long t = dateObject.getTime(); t < (dateObject.getTime() + 24 * 60 * 60 * 1000); t = t + increase){
	totalColumns++;
}
System.out.println("from="+date+", to date="+endDate+" east 8 time zone, totalColumns=" + totalColumns);
for(long t = dateObject.getTime(); t < (dateObject.getTime() + 24 * 60 * 60 * 1000); t = t + increase){
	span = t + increase;
	minute++;
	hit = false;
	speed = 0;
	count = 0;
	for(int i = 0; i < result.size(); i++){
		Report rp = result.get(i);
		if(!hit && (t <= rp.getCreateTime().getTime())){
			hit = true;
		}
		if(hit && rp.getCreateTime().getTime() < span){
			count++;
			speed = speed + rp.getSpeed();
		}else if(hit){
			break;
		}
	}
	//if(count > 0)
		//System.out.println("speed=" + speed+",count=" + count);
	if(!hit){
		if(totalColumns == (minute))
			sb.append("["+minute+",0]"); // 速度为0，没有记录
		else
			sb.append("["+minute+",0],"); // 速度为0，没有记录
	}else{
		if(totalColumns == (minute))
			sb.append("["+minute+","+(count > 0 ? nf.format(speed/count) : 0)+"]"); // 平均速度
		else
			sb.append("["+minute+","+(count > 0 ? nf.format(speed/count) : 0)+"],"); // 平均速度	
	}
	if((totalColumns / 12 * currentCol) == (minute)
			|| (totalColumns == (minute))){ // 12个柱子之一的时间
		time.setTime(span);
		if(totalColumns == (minute))
			label.append("["+minute+",'"+localFormat.format(time)+"']");
		else
			label.append("["+minute+",'"+localFormat.format(time)+"'],");
		currentCol++;
	}
}
sb.append("]");
label.append("]");
//out.write(sb.toString());
out.println(sb.toString());
out.print(label.toString());
System.out.println("{data:" + sb.toString()+", text:"+label.toString()+"}");
com.brother.database.DataBase.closeThreadConnection();
%>